package com.geek.gkfood.util.el;

import com.googlecode.aviator.AviatorEvaluator;
import com.googlecode.aviator.Expression;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * <h1>Aviator 表达式工具类。</h1><br/>
 *
 * <h2>参考链接：https://www.yii666.com/blog/594269.html</h2>
 *
 * @author hmilyylimh
 * ^_^
 * @version 0.0.1
 * ^_^
 * @date 2023/9/3
 */
public class AviatorUtils {

    /**
     * <h2根据表达式 expression 从 root 根对象中获取值进行计算，最终得到表达式计算后的结果值。</h2>
     *
     * @throws
     * @param: root
     * @param: expression
     * @return: T
     * @author hmilyylimh
     * @date: 2023/9/7
     */
    public static <T> T getValue(Map<String, Object> root, String expression) {
        Expression exp = AviatorEvaluator.compile(expression, true);
        return (T) exp.execute(root);
    }

    public static void main(String[] args) {

        System.out.println("-----------------------------------------------------------------");
        System.out.println("算术表达式【1+1】： " + AviatorEvaluator.execute("1+1"));
        System.out.println("逻辑表达式【1==1】： " + AviatorEvaluator.execute("1==1"));
        System.out.println("三元表达式【1==1 ? '对' : '错'】： " + AviatorEvaluator.execute("1==1 ? '对' : '错'"));

        System.out.println("正则表达式： " +  AviatorEvaluator.execute("'killme@2008gmail.com'=~/([\\w0-8]+@\\w+[\\.\\w+]+)/ ? $1:'unknow'"));
        System.out.println("-----------------------------------------------------------------");

        System.out.println("函数调用【6的3次方】： " + AviatorEvaluator.execute("math.pow(6,3)"));
        System.out.println("求字符串长度： " +       AviatorEvaluator.execute("string.length('hello')"));
        System.out.println("判断字符串是否包含字符串： " +     AviatorEvaluator.execute("string.contains('hello','h')"));
        System.out.println("是否以子串开头： " +     AviatorEvaluator.execute("math.pow(-3,2)"));
        System.out.println("求n次方： " +        AviatorEvaluator.execute("math.sqrt(14.0)"));
        System.out.println("正弦函数： " +      AviatorEvaluator.execute("math.sin(20)"));

        System.out.println("-----------------------------------------------------------------");


        Map env = new HashMap(16);
        env.put("yourname","aviator");
        String result3 = (String)AviatorEvaluator.execute(" 'hello ' + yourname ", env);
        System.out.println("变量和字符串相加:"+ result3);

        System.out.println("-----------------------------------------------------------------");

    }

    private static Map<String, Object> createMap() {
        Map<String, Object> map = new HashMap<>();
        map.put("R001", new BigDecimal("1"));
        map.put("R002", new BigDecimal("2"));
        map.put("R003", new BigDecimal("3"));

        Map<String, BigDecimal> map1 = new HashMap<>();
        map1.put("amt1", new BigDecimal("1"));
        map.put("R0001", map1);

        Map<String, BigDecimal> map2 = new HashMap<>();
        map2.put("amt2", new BigDecimal("2"));
        map.put("R0002", map2);

        Map<String, BigDecimal> map3 = new HashMap<>();
        map3.put("amt3", new BigDecimal("3"));
        map.put("R0003", map3);
        return map;
    }
}